﻿using System;
using System.Collections.Generic;
using System.Text;
using VideoFile;
using IConfig;
using VideoConfig;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using IAlarm;

namespace AlarmManager
{
    public sealed class FileAlarmWriter : IAlarmWriter
    {
        #region IEventWriter 成员
        public bool WriteGroup(GroupInfo group)
        {
            IConfigReader _config = FileConfigReader.SingleConfig;
            string root = _config[group.Area, group.Device]["event"];
            if (root == string.Empty) return false;
            string groupFile = FilePath.GetDateTimePathTimeFile(root, group.Area, group.Device, group.GroupTime, group.GroupTime, "group");

            FileInfo fiGroup = new FileInfo(groupFile);
            //若报警组目录不存在，则创建该目录
            if (!fiGroup.Directory.Exists) fiGroup.Directory.Create();

            using (StreamWriter swGroup = File.CreateText(fiGroup.FullName))
            {
                //存储报警组
                swGroup.WriteLine(group.GroupID);
                swGroup.WriteLine(group.Area);
                swGroup.WriteLine(group.Device);
                swGroup.WriteLine(group.HorizonAngle);
                swGroup.WriteLine(group.VerticalAngle);
                swGroup.WriteLine(group.AnalyzeCount);
                swGroup.WriteLine((int)group.GroupOperate);
                for (int i = 0; i < group.AlarmCount; i++)
                {
                    swGroup.WriteLine(group[i].AlarmID);
                }
            }

            return true;
        }

        public bool WriteAlarm(GroupInfo group, AlarmInfo alarm, Bitmap frame)
        {
            IConfigReader _config = FileConfigReader.SingleConfig;
            string root = _config[group.Area, group.Device]["event"];
            if (root == string.Empty) return false;
            string groupPath = FilePath.GetDateTimePath(root, group.Area, group.Device, group.GroupTime);

            //若报警组目录不存在，则创建该目录
            if (!Directory.Exists(groupPath)) Directory.CreateDirectory(groupPath);

            string alarmFile = FilePath.GetDateTimePathTimeFile(root, group.Area, group.Device, group.GroupTime, alarm.AlarmTime, "alarm");
            string frameFile = FilePath.GetDateTimePathTimeFile(root, group.Area, group.Device, group.GroupTime, alarm.AlarmTime, "jpg");

            try
            {
                frame.Save(frameFile);
            }
            catch { return false; }


            FileInfo fiAlarm = new FileInfo(alarmFile);

            using (StreamWriter swAlarm = File.CreateText(fiAlarm.FullName))
            {
                //存储报警
                {
                    swAlarm.WriteLine(alarm.GroupID);
                    swAlarm.WriteLine(alarm.FrameIndex);
                    swAlarm.WriteLine(alarm.AlarmID);
                    int regionLength = Math.Min(alarm.AlarmRegion.Length, alarm.AlarmRegionType.Length);
                    for (int i = 0; i < regionLength; i++)
                    {
                        swAlarm.WriteLine(string.Format("{0},{1},{2},{3},{4}", alarm.AlarmRegion[i].X,
                            alarm.AlarmRegion[i].Y,
                            alarm.AlarmRegion[i].Width,
                            alarm.AlarmRegion[i].Height,
                            (int)alarm.AlarmRegionType[i]
                            ));
                    }
                }
            }


            return true;
        }

        #endregion
    }
}
